package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsQuickExitDetailedMapper;
import com.jy.wms.dao.WmsQuickExitMapper;
import com.jy.wms.pojo.WmsQuickExitDetailed;
import com.jy.wms.pojo.WmsStockMoveDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
    * @ClassName: WmsQuickExitDetailedService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Liyi
    * @Date Sat Apr 28 09:11:38 CST 2018
    */

    @Service
    public class WmsQuickExitDetailedService extends BaseService<WmsQuickExitDetailedMapper,WmsQuickExitDetailed> {

        @Autowired
        private WmsQuickExitMapper wmsQuickExitdMapper;

     /**
     * 插入一条记录
     * @param entity 实体对象
     * @return int
     */
     @BaseServiceDescribe(moduleName = "快速出库明细", btnName = "新增快速出库明细信息")
     @Transactional
    public void baseInsert(List<WmsQuickExitDetailed> entity, HttpSession session)throws SucessException,Exception {
        if(wmsQuickExitdMapper.baseSelectByPrimaryKey(entity.get(0).getKsckid()).getFyzt().equals("0")){
            StringBuffer message = new StringBuffer();
            List<WmsQuickExitDetailed> list = new ArrayList<>();
            for (WmsQuickExitDetailed wmsQuickExitDetailed : entity) {
                //如果库存数量小于要扣减的数量则不允许新增
                if(dao.getKykcsl(wmsQuickExitDetailed.getHpkcid()) < wmsQuickExitDetailed.getZysl()){
                    Map<String,Object> map =  dao.getGoodsByKcid(wmsQuickExitDetailed.getHpkcid());
                    message.append("货品名称：" + map.get("hpmc").toString() + " 库位名称："+ map.get("kwmc").toString() + ",");
                }else{
                    wmsQuickExitDetailed.setId(UUIDUtils.uuid());
                    list.add(wmsQuickExitDetailed);
                }
            }
            dao.baseInsertAll( list );
            throw new SucessException(message.length() > 0 ? message.toString() + "库存不足，扣减失败" : "");
        }else{
            throw new ServiceException("改快速出库单已下发，无法新增明细信息");
        }

    }

    /**
     * 查询可移动库存
     * @param wmsStockMoveDetailed
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "快速出库明细", btnName = "查询可用库存数量")
    public Page<WmsStockMoveDetailed> searchKc(WmsStockMoveDetailed wmsStockMoveDetailed) throws Exception {
        PageHelper.startPage(wmsStockMoveDetailed.getPage(),wmsStockMoveDetailed.getRows());
        return (Page<WmsStockMoveDetailed>)dao.searchKc( wmsStockMoveDetailed );
    }

   /**
    * 插入一条记录（匹配有值的字段）
    * @param entity 实体对象
    * @return int
    */
    public Integer baseInsertSelective(WmsQuickExitDetailed entity){
         return dao.baseInsertSelective( entity);
    }

   /**
    * 删除（根据主键ID删除）
    * @param id 主键
    * @return int
    */
    public Integer baseDeleteByPrimaryKey(String id){
         return dao.baseDeleteByPrimaryKey( id);
    }

   /**
    * 删除（根据ID 批量删除）
    * @param idList 主键ID列表
    * @return int
    */
   @BaseServiceDescribe(moduleName = "快速出库明细", btnName = "删除快速出库明细信息")
    public void baseDeleteBathPrimaryKeys( List<String> idList)throws SucessException,Exception {
       WmsQuickExitDetailed wmsQuickExitDetailed = dao.baseSelectByPrimaryKey(idList.get(0));
       if(wmsQuickExitdMapper.baseSelectByPrimaryKey(wmsQuickExitDetailed.getKsckid()).getFyzt().equals("0")){
           dao.baseDeleteBathPrimaryKeys( idList );
           throw new SucessException();
       }else{
           throw new ServiceException("改快速出库单已下发，无法新增明细信息");
       }
    }

    @BaseServiceDescribe(moduleName = "快速出库明细", btnName = "根据ksckid查询快速出库明细信息")
    public List<Map<String,Object>> getList(String ksckid) throws Exception {
        return dao.getList(ksckid);
    }

    /**
    * 分页查询
    * @param entity 实体
    * @return Page<WmsQuickExitDetailed>
    */
    public Page<WmsQuickExitDetailed> baseSelectPage(WmsQuickExitDetailed entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        return (Page<WmsQuickExitDetailed>)dao.baseSelectPage( entity );
    }

   /**
    * 查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsQuickExitDetailed
    */
    public WmsQuickExitDetailed baseSelectByPrimaryKey(String id ){
         return dao.baseSelectByPrimaryKey( id );
    }

   /**
    * 查询（根据ID 批量查询）
    * @param idList 主键ID列表
    * @return list
    */
    public List<WmsQuickExitDetailed> baseSelectBathPrimaryKeys(List<String> idList ){
         return dao.baseSelectBathPrimaryKeys( idList );
    }

   /**
    *修改 （匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKeySelective(WmsQuickExitDetailed entity){
         return dao.baseUpdateByPrimaryKeySelective( entity);
    }

   /**
    *修改（根据主键ID修改）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKey(WmsQuickExitDetailed entity){
         return dao.baseUpdateByPrimaryKey( entity);
    }
}
